package com.zelin.controller;

import com.zelin.service.ClassesService;
import com.zelin.util.PageResult;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import com.zelin.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/4/10 12:16
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 查询所有学生
     * @return
     */
    @RequestMapping("/list")
    public List<Student> findAll(){
        return studentService.findAll();
    }

    /**
     * 分页查询，但不带有条件查询功能
     * @param page 代表当前页
     * @param pagesize 代表每页大小
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult<Student> findByPage(int page, int pagesize){
        return studentService.findByPage(page,pagesize);
    }

    /**
     * 条件查询带分页功能
     * @param page     当前页
     * @param pagesize 每页大小
     * @param student  封装的查询条件
     * @return
     */
    @RequestMapping("/search")
    //@RequestBody :将前面的json串转换为java对象，这里就是转换为student对象
    public  PageResult<Student> search(int page, int pagesize,@RequestBody Student student){
        return studentService.search(page,pagesize,student);
    }

    /**
     * 添加学生
     * @param student 要添加的学生对象
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Student student){
        try {
            studentService.add(student);
            return new Result(true,"添加学生成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加学生失败！");
        }
    }

    /**
     * 修改学生
     * @param student 要修改的学生对象
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Student student){
        try {
            studentService.update(student);
            return new Result(true,"修改学生成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改学生失败！");
        }
    }

    /**
     * 根据学号删除学生
     * @param sid
     * @return
     */
    @RequestMapping("/del")
    public Result del(String sid){
        try {
            studentService.delete(sid);
            return new Result(true,"删除学生成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除学生失败！");
        }
    }

    /**
     * 批量删除学生
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel")
    public Result batchDel(String[] ids){
        try {
            System.out.println("ids:" + ids);
            studentService.batchDel(ids);
            return new Result(true,"批量删除学生成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"批量删除学生失败！");
        }
    }

}
